package jrain.fw.script.operator;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

import jrain.fw.script.tools.ELException;
import jrain.fw.script.tools.Logger;

public class EmptyOperator extends UnaryOperator {

	public static final EmptyOperator SINGLETON = new EmptyOperator();

	public EmptyOperator() {
	}

	public String getOperatorSymbol() {
		return "empty";
	}

	public Object apply(Object pValue, Object pContext, Logger pLogger) throws ELException {
		// NULL空
		if (pValue == null) {
			return PrimitiveObjects.getBoolean(true);
		}
		// 字符空
		else if ("".equals(pValue)) {
			return PrimitiveObjects.getBoolean(true);
		}
		// 数组空
		else if (pValue.getClass().isArray() && Array.getLength(pValue) == 0) {
			return PrimitiveObjects.getBoolean(true);
		}
		// 集合空
		else if (pValue instanceof Collection && ((Collection<?>) pValue).isEmpty()) {
			return PrimitiveObjects.getBoolean(true);
		}
		// Map空
		else if (pValue instanceof Map && ((Map<?, ?>) pValue).isEmpty()) {
			return PrimitiveObjects.getBoolean(true);
		}
		// 其他情况非空
		else {
			return PrimitiveObjects.getBoolean(false);
		}
	}

}
